<template>
	<view class="page">
		<view class="showcase">
			<view class="showcase__item">
				<view class="showcase__item-label">心率</view>
				<view class="showcase__item-value">{{heartRate ? heartRate : '--'}}次/分钟</view>
			</view>
			<view class="showcase__item">
				<view class="showcase__item-label">血氧</view>
				<view class="showcase__item-value">{{bloodOxygen ? bloodOxygen : '--'}}%</view>
			</view>
		</view>
		
		<view class="showcase">
			<view class="showcase__item">
				<view class="showcase__item-label">舒张压</view>
				<view class="showcase__item-value">{{highPressure ? highPressure : '--'}}mmHg</view>
			</view>
			<view class="showcase__item">
				<view class="showcase__item-label">收缩压</view>
				<view class="showcase__item-value">{{lowPressure ? lowPressure : '--'}}mmHg</view>
			</view>
		</view>
		
		<view class="button-box">
			<view class="button-box__item">
				<button type="primary" @click="startScanDevice">开始扫描</button>
				<button type="primary" @click="stopScanDevice">停止扫描</button>
			</view>
			
			<view class="button-box__item">
				<button type="primary" @click="connectDevice">连接设备</button>
				<button type="primary" @click="disconnectWatch">断开连接</button>
			</view>
			
			<view class="button-box__item">
				<button type="primary" @click="confirmDevicePwd">校验密码</button>
				<button type="primary" @click="syncPersonInfo">同步用户信息</button>
			</view>
			
			<view class="button-box__item">
				<button type="primary" @click="startDetectHeart">开始测量心率</button>
				<button type="primary" @click="stopDetectHeart">停止测量心率</button>
			</view>
			
			<view class="button-box__item">
				<button type="primary" @click="startDetectSPO2H">开始测量血氧</button>
				<button type="primary" @click="stopDetectSPO2H">停止测量血氧</button>
			</view>
			
			<view class="button-box__item">
				<button type="primary" @click="startDetectBP">开始测量血压</button>
				<button type="primary" @click="stopDetectBP">停止测量血压</button>
			</view>
			
			<view class="button-box__item">
				<button type="primary" @click="readSportStep">读取计步数据</button>
				<button type="primary" @click="readBattery">读取电量</button>
			</view>
		</view>
	</view>
</template>

<script>
	const VpBluetooth = uni.requireNativePlugin("UUCoder-VpBluetooth");
	
	export default {
		data() {
			return {
				heartRate: '',
				bloodOxygen: '',
				highPressure: '',
				lowPressure: ''
			}
		},
		mounted() {
			/**
			 * VpBluetooth 初始化
			 */
			const result = VpBluetooth.init(event => {
				VpBluetooth.log("VpBluetooth.init.callback: " + JSON.stringify(event));
				console.log("VpBluetooth.init.callback: " + JSON.stringify(event));
				
				if (event.name) {
					switch(event.name) {
						case "onSearchStarted":
							VpBluetooth.log("开始扫描设备");
							console.log("开始扫描设备");
							break;
						case "onDeviceFounded":
							VpBluetooth.log("扫描到设备：" + JSON.stringify(event.data));
							console.log("扫描到设备：" + JSON.stringify(event.data));
							break;
						case "onSearchStopped":
							VpBluetooth.log("停止扫描设备");
							console.log("停止扫描设备");
							break;
						case "onSearchCanceled":
							VpBluetooth.log("取消扫描设备");
							console.log("取消扫描设备");
							break;
					}
				}
			});
			VpBluetooth.log("VpBluetooth.init: " + result);
			console.log("VpBluetooth.init: " + result);
		},
		methods: {
			/**
			 * 开始扫描设备
			 */
			startScanDevice() {
				VpBluetooth.startScanDevice();
			},
			/**
			 * 停止扫描设备
			 */
			stopScanDevice() {
				VpBluetooth.stopScanDevice();
			},
			/**
			 * 连接设备
			 */
			connectDevice() {
				const isIOS = uni.getSystemInfoSync().platform === "ios";
				VpBluetooth.log("System platform: " + isIOS);
				console.log("System platform: " + isIOS);
				
				const result = VpBluetooth.connectDevice(isIOS ? "8D60FEF7-7B19-F723-C660-F5312CA844AB" : "E1:C3:7F:97:08:6D", result => {
					VpBluetooth.log("VpBluetooth.connectDevice.callback: " + JSON.stringify(result));
					console.log("VpBluetooth.connectDevice.callback: " + JSON.stringify(result));
					
					if (result.state !== 'SUCCESS') {
						uni.showToast({
							title: "连接失败",
							icon: "none"
						});
						return;
					}
					
					switch(result.type) {
						case "connectResponse":
							uni.showToast({
								title: "设备连接成功",
								icon: "success"
							});
							break;
						case "notifyResponse":
							uni.showToast({
								title: "设备监听就绪",
								icon: "success"
							});
							break;
					}
				});
				VpBluetooth.log("VpBluetooth.connectDevice: " + result);
				console.log("VpBluetooth.connectDevice: " + result);
			},
			/**
			 * 校验密码
			 */
			confirmDevicePwd() {
				const result = VpBluetooth.confirmDevicePwd("0000", result => {
					VpBluetooth.log("VpBluetooth.confirmDevicePwd.callback: " + JSON.stringify(result));
					console.log("VpBluetooth.confirmDevicePwd.callback: " + JSON.stringify(result));
					
					switch(result.type) {
						case "onPwdDataChange":
							VpBluetooth.log("获取到密码信息");
							console.log("获取到密码信息");
							uni.showModal({
								title: "onPwdDataChange",
								content: JSON.stringify(result.data),
								showCancel: false
							});
							break;
						case "onFunctionSupportDataChange":
							VpBluetooth.log("获取到设备信息");
							console.log("获取到设备信息");
							uni.showModal({
								title: "onFunctionSupportDataChange",
								content: JSON.stringify(result.data),
								showCancel: false
							});
							break;
					}
				});
				VpBluetooth.log("VpBluetooth.confirmDevicePwd: " + result);
				console.log("VpBluetooth.confirmDevicePwd: " + result);
			},
			/**
			 * 同步用户信息
			 */
			syncPersonInfo() {
				VpBluetooth.syncPersonInfo(1, 168, 58, 24, 10000, result => {
					VpBluetooth.log("VpBluetooth.syncPersonInfo.callback: " + JSON.stringify(result));
					console.log("VpBluetooth.syncPersonInfo.callback: " + JSON.stringify(result));
					
					if (result.status === "OPRATE_SUCCESS") {
						uni.showToast({
							title: "同步用户信息成功",
							icon: "none"
						});
					} else {
						uni.showToast({
							title: `同步用户信息失败[${result.status}]`,
							icon: "none"
						});
					}
				});
			},
			/**
			 * 开始测量心率
			 */
			startDetectHeart() {
				VpBluetooth.startDetectHeart(result => {
					VpBluetooth.log("VpBluetooth.startDetectHeart.callback: " + JSON.stringify(result));
					console.log("VpBluetooth.startDetectHeart.callback: " + JSON.stringify(result));
					this.heartRate = result.value;
				});
			},
			/**
			 * 停止测量心率
			 */
			stopDetectHeart() {
				VpBluetooth.stopDetectHeart();
			},
			/**
			 * 开始测量血氧
			 */
			startDetectSPO2H() {
				VpBluetooth.startDetectSPO2H(result => {
					VpBluetooth.log("VpBluetooth.startDetectSPO2H.callback: " + JSON.stringify(result));
					console.log("VpBluetooth.startDetectSPO2H.callback: " + JSON.stringify(result));
					this.bloodOxygen = result.value;
				});
			},
			/**
			 * 停止测量血氧
			 */
			stopDetectSPO2H() {
				VpBluetooth.stopDetectSPO2H();
			},
			/**
			 * 开始测量血压
			 */
			startDetectBP() {
				VpBluetooth.startDetectBP(result => {
					VpBluetooth.log("VpBluetooth.startDetectBP.callback: " + JSON.stringify(result));
					console.log("VpBluetooth.startDetectBP.callback: " + JSON.stringify(result));
					
					if (result.status === 'STATE_BP_NORMAL') {
						if (result.progress < 100) {
							uni.showLoading({
								title: `血压测量：${result.progress}%`
							});
						} else {
							uni.showToast({
								title: "测量完成",
								icon: "success"
							});
							this.highPressure = result.highPressure;
							this.lowPressure = result.lowPressure;
						}
					} else {
						uni.showToast({
							title: "设备繁忙，血压检测失败",
							icon: "icon"
						});
					}
				});
			},
			/**
			 * 停止测量血压
			 */
			stopDetectBP() {
				VpBluetooth.stopDetectBP();
				uni.hideLoading();
			},
			/**
			 * 读取计步数据
			 */
			readSportStep() {
				VpBluetooth.readSportStep(result => {
					VpBluetooth.log("VpBluetooth.readSportStep.callback: " + JSON.stringify(result));
					console.log("VpBluetooth.readSportStep.callback: " + JSON.stringify(result));
				});
			},
			/**
			 * 读取计步数据
			 */
			readBattery() {
				VpBluetooth.readBattery(result => {
					VpBluetooth.log("VpBluetooth.readBattery.callback: " + JSON.stringify(result));
					console.log("VpBluetooth.readBattery.callback: " + JSON.stringify(result));
				});
			},
			/**
			 * 断开连接
			 */
			disconnectWatch() {
				VpBluetooth.disconnectWatch();
			},
		}
	}
</script>

<style>
	.showcase {
		padding: 40rpx 0;
		margin: 0 32rpx;
		display: flex;
	}
	
	.showcase:not(:first-child) {
		border-top: 1rpx solid #ddd;
	}
	
	.showcase__item {
		flex: 1;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	
	.showcase__item-label {
		font-size: 28rpx;
		line-height: 40rpx;
		color: #888;
		text-align: center;
	}
	
	.showcase__item-value {
		font-size: 40rpx;
		line-height: 56rpx;
		color: black;
		font-weight: 700;
		text-align: center;
		margin-top: 12rpx;
	}
	
	.button-box {
		padding: 40rpx 32rpx;
		overflow: hidden;
	}
	
	.button-box:not(:first-child) {
		padding-top: 0;
	}
	
	.button-box__item {
		display: flex;
	}
	
	.button-box__item button {
		flex: 1;
	}
	
	button + button {
		margin-left: 20rpx;
	}
	
	.button-box__item:not(:first-child) {
		margin-top: 20rpx;
	}
</style>
